# 检查 T 字形区域是否有效且至少有一个 1
def is_valid_t_shape(matrix, x, y, n):
    # 定义四种 T 字形的偏移量
    t_shapes = [
        [(x - 1, y), (x, y), (x + 1, y), (x, y + 1)],
        [(x, y - 1), (x, y), (x, y + 1), (x + 1, y)],
        [(x - 1, y), (x, y), (x + 1, y), (x, y - 1)],
        [(x, y - 1), (x, y), (x, y + 1), (x - 1, y)]
    ]
    for shape in t_shapes:
        valid = True
        has_one = False
        for i, j in shape:
            # 检查坐标是否越界
            if i < 0 or i >= n or j < 0 or j >= n:
                valid = False
                break
            if matrix[i][j] == '1':
                has_one = True
        if valid and has_one:
            return shape
    return None

# 执行 T 字形操作，将区域内元素置为 0
def perform_t_shape(matrix, shape):
    for i, j in shape:
        matrix[i][j] = '0'

# 回溯函数，尝试所有可能的 T 字形操作
def backtrack(matrix, n):
    max_ops = 0
    for i in range(n):
        for j in range(n):
            shape = is_valid_t_shape(matrix, i, j, n)
            if shape:
                # 备份当前矩阵
                original_matrix = [row[:] for row in matrix]
                perform_t_shape(matrix, shape)
                # 递归调用 backtrack 并更新最大操作次数
                ops = 1 + backtrack(matrix, n)
                max_ops = max(max_ops, ops)
                # 恢复矩阵
                matrix = [row[:] for row in original_matrix]
    return max_ops

# 读取数据组数
D = int(input())
for _ in range(D):
    # 读取矩阵大小
    n = int(input())
    matrix = [list(input()) for _ in range(n)]
    # 调用 backtrack 函数计算最大操作次数
    result = backtrack(matrix, n)
    print(result+1)